integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
list gMenu=[];
list statesMenu=[1,0,0,0,0,0,0,0];
list gAvatarAllow;
key ToucherID;
integer gMenuLink =1337;
integer gDemon =1338;
integer gPlugin =1339;
integer gVic =1329;
integer channel_dialog;
integer listen_id;
string msg ="Enviorment Settings";
list sideMenu=[" ","Back"," "];
integer changes;

llRlvSay(string command)
{
	if(attachment==TRUE)
	{
		llOwnerSay("@"+command);
	}
	else
	{
		llSay(relaychannel,cmdname + "," + (string)victim  + "," + "@"+command);
	}
}

MakeRLVState()
{
	if(attachment==TRUE)
	{
		if(llList2Integer(statesMenu,0)==1)makeDefault();
		if(llList2Integer(statesMenu,1)==1)makeBlack();
		if(llList2Integer(statesMenu,2)==1)makeWhite();
		if(llList2Integer(statesMenu,3)==1)makeStrange();
		if(llList2Integer(statesMenu,4)==1)makeRandom();
		if(llList2Integer(statesMenu,5)==1)llRlvSay("setenv=n");
	}
}
makeDefault()
{
	statesMenu=[1,0,0,0,0,changes,0,0];
	llRlvSay("setenv_scenegamma:1.0=force");
	llRlvSay("setenv_ambientr:0.34=force");
	llRlvSay("setenv_ambientg:0.35=force");
	llRlvSay("setenv_ambientb:0.35=force");
	llRlvSay("setenv_ambienti:0.35=force");
}

makeBlack()
{
	statesMenu=[0,1,0,0,0,changes,0,0];
	llRlvSay("setenv_scenegamma:0.0=force");
	llRlvSay("setenv_ambientr:0.0=force");
	llRlvSay("setenv_ambientg:0.0=force");
	llRlvSay("setenv_ambientb:0.0=force");
	llRlvSay("setenv_ambienti:0.0=force");
	
}

makeWhite()
{
	statesMenu=[0,0,1,0,0,changes,0,0];
	llRlvSay("setenv_scenegamma:10.0=force");
	llRlvSay("setenv_ambientr:1.0=force");
	llRlvSay("setenv_ambientg:1.0=force");
	llRlvSay("setenv_ambientb:1.0=force");
	llRlvSay("setenv_ambienti:1.0=force");

}

makeStrange()
{
	statesMenu=[0,0,0,1,0,changes,0,0];
	llRlvSay("setenv_scenegamma:1.0=force");
	llRlvSay("setenv_ambienti:0.99=force");
	llRlvSay("setenv_ambientr:0.99=force");
	llRlvSay("setenv_ambientg:0.0=force");
	llRlvSay("setenv_ambientb:0.99=force");
	
}
makeRandom()
{
	statesMenu=[0,0,0,0,1,changes,0,0];
	integer _rand;
	_rand = (integer)llFrand(99);
	llRlvSay("setenv_scenegamma:0."+(string)_rand+"=force");
	_rand = (integer)llFrand(99);
	llRlvSay("setenv_ambienti:0."+(string)_rand+"=force");
	_rand = (integer)llFrand(99);
	llRlvSay("setenv_ambientr:0."+(string)_rand+"=force");
	_rand = (integer)llFrand(99);
	llRlvSay("setenv_ambientg:0."+(string)_rand+"=force");
	_rand = (integer)llFrand(99);
	llRlvSay("setenv_ambientb:0."+(string)_rand+"=force");
	
}
makeMenu()
{
	gMenu=[];
	gMenu+=sideMenu;
	if(llList2Integer(statesMenu,0)==1)gMenu+="[x]Default";
	else gMenu+="[ ]Default";
	if(llList2Integer(statesMenu,1)==1)gMenu+="[x]Black";
	else gMenu+="[ ]Black";
	if(llList2Integer(statesMenu,2)==1)gMenu+="[x]White";
	else gMenu+="[ ]White";
	if(llList2Integer(statesMenu,3)==1)gMenu+="[x]Strange";
	else gMenu+="[ ]Strange";
	if(llList2Integer(statesMenu,4)==1)gMenu+="[x]Random";
	else gMenu+="[ ]Random";
	if(llList2Integer(statesMenu,5)==1)gMenu+="[x]Change";
	else gMenu+="[ ]Change";
	//Than display
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,gMenu, channel_dialog);
	llSetTimerEvent(45.0);
}

llLRP(integer element_new, integer placeinlist)
{
	statesMenu = llListReplaceList(statesMenu, [element_new], placeinlist, placeinlist);
}

default {
    state_entry() 
    {
    	    
    }
    
    listen(integer channel, string name, key id, string message)
    {
    	llListenRemove(listen_id);
		llSetTimerEvent(0.0);
		if(message=="[ ]Default"){llLRP(1,0);makeDefault();makeMenu();}
		if(message=="[x]Default"){llLRP(1,0);makeDefault();makeMenu();}
		if(message=="[ ]Black"){llLRP(1,1);makeBlack();makeMenu();}
		if(message=="[x]Black"){llLRP(1,1);makeBlack();makeMenu();}
		if(message=="[ ]White"){llLRP(1,2);makeWhite();makeMenu();}
		if(message=="[x]White"){llLRP(1,2);makeWhite();makeMenu();}
		if(message=="[ ]Strange"){llLRP(1,3);makeStrange();makeMenu();}
		if(message=="[x]Strange"){llLRP(1,3);makeStrange();makeMenu();}
		if(message=="[ ]Random"){llLRP(1,4);makeRandom();makeMenu();}
		if(message=="[x]Random"){llLRP(1,4);makeRandom();makeMenu();}
		if(message=="[ ]Change"){llLRP(1,5);llRlvSay("setenv=n");makeMenu();changes=1;}
		if(message=="[x]Change"){llLRP(0,5);llRlvSay("setenv=y");makeMenu();changes=0;}
		if(message=="Back")llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_RLV", ToucherID);	
    }
    
    on_rez(integer on)
    {
    
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				makeMenu();
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str =="attachment")attachment=TRUE;
  			else if(str =="device")attachment=FALSE;
  			else if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);

  			}
  		}
  		if(num==gPlugin)
  		{
  			if(str=="ON_REZ")
  			{
  				MakeRLVState();
  			}
  			if(str=="RESET")
  			{
  				statesMenu=[1,0,0,0,0,0,0,0];
  			}
  		}
  		if(num==gVic)
  		{
  				victim=(key)str;
  				cmdname = (string)llGetKey();
  		}
  	}
  	
  	timer()
    {
    	llSetTimerEvent(0.0);
    	llListenRemove(listen_id);
    }
}
